#include "conn.hpp"

void display(string& output,std::vector<string>& title,std::vector<string>& author,std::vector<string>& createdate){
	FILE* fp = fopen("./MyRootDir/display.html","r");
	if(!fp){
		perror("open fail");
	}
	char ch = 0;
	int i = 0,j = 0;
	while( !feof(fp)){
		ch = fgetc(fp);
		if(ch == '@'){
			ch = fgetc(fp);
			if(ch == '1'){
                if(i < title.size()){
				    output += title[i];
				    ++i;
                }else{
                    output += "nomore";
                }
            }else{
				output += "--author:";
                if(j < author.size()){
				    output += author[j];
                }
				output += "--data:";
                if(j < createdate.size()){
				    output += createdate[j];
                }
                ++j;
			}
		}else 
            output.push_back(ch);
	}
}

int main(){
    StringUtil::UrlParam params;
    MYSQL* conn;
    Init(params,conn); 

    //根据业务需要进行计算
    string keyword = params["keyword"];
    stringstream ss;
    string output;
    
    std::vector<string> title,author,createdate;
    OperateNews::SearchNews(conn,keyword,title,author,createdate);
    display(output,title,author,createdate);
    
    ss << output;
    HttpResponse(ss.str());
    return 0;
}
